/*
* @Author: wuxianyu
* @Date:   2017-02-16 09:31:28
* @Last Modified by:   zrug
* @Last Modified time: 2017-02-26 17:01:00
*/
angular.module('app')
  .directive('uploadimgapply', function($compile, ngDialog, uiLoad) {   
    return {
      restrict: 'E',
      templateUrl: 'js/module/upload-direct/uploadImgApply.html',
      replace: true             
    };
  })
  .controller('fileUploadCtrlApply', ['$element', '$scope', 'FileUploader', 'uploadAddress', 'signature',  function($element, $scope, FileUploader, uploadAddress, signature,uploadConf2) {
    
    var uploader = $scope.uploader = new FileUploader({
        url: uploadAddress
    });    
    $scope.add = function() {
        $scope.column2 =  uploadConf2.column2;          
        $scope.maxImgNum = uploadConf2.maxImgNum;
        $scope.imgNew2 = uploadConf2.imgNew2;
    }
     $scope.change = function() {
       uploadConf2.column2 =$scope.column2;
       uploadConf2.maxImgNum = $scope.maxImgNum;
       uploadConf2.imgNew2 = $scope.imgNew2;
    }

    uploader.onWhenAddingFileFailed = function(item /*{File|FileLikeObject}*/ , filter, options) {
        //console.info('onWhenAddingFileFailed', item, filter, options);
    };
    uploader.onAfterAddingFile = function(fileItem) {
        //console.info('onAfterAddingFile', fileItem);
        uploader.uploading=1;
    };
    uploader.onAfterAddingAll = function(addedFileItems) {
        //console.info('onAfterAddingAll', addedFileItems);
    };
    uploader.onBeforeUploadItem = function(item) {
        return item.formData = [signature.run('9999', {})];
       // console.info('onBeforeUploadItem', item);
       uploader.uploading=1;
    };
    uploader.onProgressItem = function(fileItem, progress) {
        //console.info('onProgressItem', fileItem, progress);
    };
    uploader.onProgressAll = function(progress) {
        //console.info('onProgressAll', progress);
    };
    uploader.onSuccessItem = function(fileItem, response, status, headers) {
        //console.info('onSuccessItem', fileItem, response, status, headers);
        // console.info('uploader', uploader);
        uploader.uploading=0;
    };
    uploader.onErrorItem = function(fileItem, response, status, headers) {
        //console.info('onErrorItem', fileItem, response, status, headers);
    };
    uploader.onCancelItem = function(fileItem, response, status, headers) {
        //console.info('onCancelItem', fileItem, response, status, headers);
    };
    uploader.onCompleteItem = function(fileItem, response, status, headers) {
        
        if($scope.indexSelect != -1){
            $scope.imgNew2[$scope.typeSelect].value.splice($scope.indexSelect, 1, response.res.data.imgList[0] );            
        }else{
            $scope.imgNew2[$scope.typeSelect].value.push (response.res.data.imgList[0]) ;
        }     
        $('#uploaderFileInput2').val("");        
    };
   
    $scope.remove = function(index,indexParent) {		
			$scope.imgNew2[indexParent].value.splice(index, 1);		
	}
    $scope.typeSelect = '';
   $scope.indexSelect = '';
    $scope.add = function(index) {	
            $scope.typeSelect = index;
            $scope.indexSelect = -1;
			// $('#uploaderFileInput2').click();              
            //angular.element('#uploaderFileInput2').click();  
            document.getElementById('uploaderFileInput2').click();  
	}
    $scope.edit = function(index,indexParent) {	
            $scope.typeSelect = indexParent;
             $scope.indexSelect = index;			 
            document.getElementById('uploaderFileInput2').click();  
	}
    // $scope.readFile = function(){
    //          $scope.selectedFile = changeEvent.target.files[0];
    //         console.log( 1111);
    //         setTimeout("scope.uploader.uploadAll(); ",20);      
    // }
    $scope.readFile = function(changeEvent){
              var typeTure = true, sizeTrue = true;
              var str = 'image';
              
              for(var i=0;i<100;i++){
                   var fileGet = changeEvent.target.files[i];
                   if(fileGet){
                        if(fileGet.type.toLowerCase().indexOf(str) === -1){                              
                               typeTure = false;
                               break;
                        }
                        if (fileGet.size > 0.3 * 1024*1024) {
                              sizeTrue = true;
                              alert('上传的图片文件大小超过 3M 可能会影响上传速度');
                              break;
                        }
                   }else{
                       break;
                   }                  
              }            
            if(typeTure && sizeTrue){
                setTimeout("$('#itemUpload2').click();",20);  
            }else if (!typeTure) {
                 $('#uploaderFileInput2').val("");   
                alert('上传的图片文件不能包含其他格式文件')
            }
            // else if (!sizeTrue) {
            //      $('#uploaderFileInput2').val("");   
            //     alert('上传的图片文件超过 3M 可能会影响上传速度');
            // }           
    }
}]);
